import os
import random
import xml.etree.ElementTree as ET
from global_var import getPath

from utils.utils import get_classes

# VOCdevkit_path = 'niaochao'
if __name__ == "__main__":
    xmlfilepath = os.path.join(getPath(), 'Annotations')
    xmlfiles = os.listdir(xmlfilepath)
    fileOutPath = os.path.join(getPath(), 'Annotations_out')
    if not os.path.exists(fileOutPath):
        os.mkdir(fileOutPath)
    
    for fileName in xmlfiles:
        # with open(os.path.join(VOCdevkit_path, fileName), "w") as new_f:
        tree = ET.ElementTree

        tree = ET.parse(os.path.join(xmlfilepath, fileName))
        # print(tree)
        # tree.write(os.path.join(xmlfilepath,"1.xml"),encoding="utf-8",xml_declaration=False)
        root = tree.getroot()

        needwrite = False
        for obj in root.findall('object'):
            node = obj.find('name')
            #obj_name = obj.find('name').text
            if (node.text == "玻璃绝缘子自爆"):
                node.text = 'ins_zb'
                needwrite = True
            # if (node.text == "car"):
            #     node.text = 'crane'
            #     needwrite = True
        if needwrite:
            tree.write(os.path.join(fileOutPath, fileName),encoding="utf-8", xml_declaration=False)
            print(fileName)
